package com.xrit.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.pojo.Member;
import com.xrit.query.QueryPageBean;
import com.xrit.result.PageResult;
import com.xrit.result.Result;
import com.xrit.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 会员管理
 */
@RestController
@RequestMapping("member")
public class MemberController {

    @Reference
    private MemberService memberService;

    /**
     * 删除
     * @param id
     * @return
     * @throws CustomException
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        int row = memberService.delete(id);
        if (row>0){
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }else{
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    /**
     * 修改
     * @param member
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Member member){
        int row = memberService.update(member);
        if (row>0){
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }else{
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 添加
     * @param member
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        int row = memberService.add(member);
        if (row>0){
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }else{
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 列表支持分页+条件
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult page = memberService.findByPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Member> list = memberService.findAll();
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Member byId = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,byId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
    }
}
